package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.http.HttpRequest;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.dao.EventDAO;
import com.cip.master.model.Album;
import com.cip.master.model.Event;
import com.cip.master.service.EventService;

public class EventServiceImpl implements EventService {
	
	private EventDAO eventDAO;
	private VelocityEngine velocityEngine;
	
	public String getEventGridList(Locale locale,HttpServletRequest request)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Event> eventList = getAllEvent();
			
			List<Map<String,Object>> eventArrayList = new ArrayList<Map<String,Object>>();
			
			for (Event event : eventList) {
				Map<String,Object> eventObj = new HashMap<String, Object>();
				if(request.getSession().getAttribute("role")== "ROLE_SUPER_ADMIN" || request.getSession().getAttribute("role")== "ROLE_ADMIN"){
					eventObj.put("name", "<a href='/editEvent?event.id="+event.getId()+"' style='text-decoration:underline;color:blue;'>"+event.getName()+"</a>");
				}else{
					eventObj.put("name", event.getName());
				}
				eventObj.put("status", event.getStatus());
				
				eventArrayList.add(eventObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, eventArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/** Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	private static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> eventArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, eventArrayList, createEventGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createEventGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Section List");
		context.put("gridId", "MY_EVENTLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Name','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "name", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}
	

	public EventDAO getEventDAO() {
		return eventDAO;
	}

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	public Event saveOrUpdate(Event event) {
		return eventDAO.saveOrUpdate(event);
	}

	public List<Event> getAllEvent() throws Exception {
		return eventDAO.getAllEvent();
	}

	public Event getEvent(int id) {
		return eventDAO.getEvent(id);
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
